package com.seo.controller.keyWord;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.keyword.member.sdk.KeywordGMemberService;
import com.seo.service.keyword.sdk.KeywordGService;
import com.seo.utils.ClientConst;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: GoodsController
 * @author rtc
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/keyWordG/")
public class KeyWordGMemberController {
	
	@Autowired
	private KeywordGMemberService keywordGMemberService;
	
	@Autowired
	private KeywordGService keywordGService;
	
	@Autowired
	private PageTool pageTool;

	/*
	 * 会员关键字列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam Map<String, Object> pam,HttpServletRequest request, HttpSession session) {
		log.info("进入代理关键字列表...");
		List<Map<String, Object>> venderList = keywordGMemberService.getVenderList(); //厂商列表
		List<Map<String, Object>> engineList = keywordGService.getEngineList(); //搜索引擎列表
		Integer userId = Jurisdiction.getT("id", Integer.class);
		List<Map<String, Object>> groupList =  keywordGMemberService.getGroupByUserId(userId);
		pam.put("userId", userId);
		//排名区间 1：1-10 2：11-50 3： 51-100 4: 100+
		String rankingInterval = (String) pam.get("rankingInterval");
		if(StringUtils.isNotBlank(rankingInterval)) {
			String startRanking = null;
			String endRanking = null;
			if("1".equals(rankingInterval)) {
				startRanking = "1";
				endRanking = "10";
			}else if("2".equals(rankingInterval)) {
				startRanking = "11";
				endRanking = "50";
			}else if("3".equals(rankingInterval)) {
				startRanking = "51";
				endRanking = "100";
			}else {
				startRanking = "100";
			}
			pam.put("startRanking", startRanking);
			pam.put("endRanking", endRanking);
		}		
		PageInfo<?> page = pageTool.getAutoPage(keywordGMemberService::getKeyWordsList,pam);
		request.setAttribute("page", page);
		request.setAttribute("venderList", venderList);
		request.setAttribute("groupList", groupList);
		request.setAttribute("engineList", engineList);
		request.setAttribute("pam", pam);
		return "keyWord/keyWord_list";

	}
	
	/**
	 * 会员关键字审核列表
	 * @param params
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/reviewList")
	public String reviewList(@RequestParam Map<String, Object> pam,HttpServletRequest request, HttpSession session) {
		log.info("进入会员关键词审核列表...");
		log.info("获取页面的传值userName: " + pam.get("userName") + "typeValue：" + pam.get("typeValue"));
		List<Map<String, Object>> engineList = keywordGService.getEngineList(); //搜索引擎列表
		Integer userId = Jurisdiction.getT("id", Integer.class);
		pam.put("userId", userId);
		PageInfo<?> page = pageTool.getAutoPage(keywordGMemberService::getKeyWordsReviewList,pam);
		log.info("关键词审核列表查询的数据：" + page);
		request.setAttribute("page", page);
		request.setAttribute("pam", pam);
		request.setAttribute("engineList", engineList);
		return "keyWord/keyWord_review_list";

	}
	
	/*
	 * 代理关键字列表
	 */
	@RequestMapping("/proxy/list")
	public String proxyList(@RequestParam Map<String, Object> pam,HttpServletRequest request, HttpSession session) {
		log.info("进入代理关键字列表...");
		List<Map<String, Object>> venderList = keywordGMemberService.getVenderList(); //厂商列表
		List<Map<String, Object>> engineList = keywordGService.getEngineList(); //搜索引擎列表
		Integer userId = Jurisdiction.getT("id", Integer.class);
		pam.put("userId", userId); // 用户id
		PageInfo<?> page = pageTool.getAutoPage(keywordGMemberService::getProxyKeyWordsList,pam);
		request.setAttribute("page", page);
		request.setAttribute("venderList", venderList);
		request.setAttribute("pam", pam);
		request.setAttribute("engineList", engineList);
		return "keyWord/proxy/keyWord_proxy_list";
	}
	
	/**
	 * 代理关键字审核列表
	 * @param params
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/proxy/reviewList")
	public String reviewProxyList(@RequestParam Map<String, Object> pam,HttpServletRequest request, HttpSession session) {
		log.info("进入会员关键词审核列表...");
		log.info("获取页面的传值userName: " + pam.get("userName") + "typeValue：" + pam.get("typeValue"));
		List<Map<String, Object>> engineList = keywordGService.getEngineList(); //搜索引擎列表
		Integer userId = Jurisdiction.getT("id", Integer.class);
		pam.put("userId", userId); // 用户id
		PageInfo<?> page = pageTool.getAutoPage(keywordGMemberService::getProxyKeyWordsReviewList,pam);
		log.info("关键词审核列表查询的数据：" + page);
		request.setAttribute("page", page);
		request.setAttribute("pam", pam);
		request.setAttribute("engineList", engineList);
		return "keyWord/proxy/keyWord_proxy_review_list";
	}
	
	
	
	
	/**
	 * 关键字审批
	 * @param pam
	 * @param session
	 * @return
	 */
	@RequestMapping("/proxy/keyWordReview")
	@ResponseBody
	public String keyWordReview(@RequestParam Map<String, Object> pam, HttpSession session) {
		log.info("进入关键词审批方法...");
		String msg = "";
		try {
			BigDecimal price1 = Jurisdiction.getT("price1", BigDecimal.class);
			pam.put("price1", price1);
			keywordGMemberService.updatekeyWordReviewDisabled(pam);
			msg = "true";
		} catch (Exception e) {
			msg = "error";
		}
		
		return msg;

	}
	
	/**
	 * 进入分组页面
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("/editGroupPage")
	public String editGroupPage(@RequestParam Map<String, Object> pam, HttpServletRequest request) {
		log.info("进入分组页面");
		Integer userId = Jurisdiction.getT("id", Integer.class);
		List<Map<String, Object>> groupList =  keywordGMemberService.getGroupByUserId(userId);
		request.setAttribute("groupList", groupList);
		request.setAttribute("ids", pam.get("ids"));
		request.setAttribute("action", "editGroup");
		return "keyWord/group_edit";
	}
	
	/**
	 * 进行分组
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping("/editGroup")
	public String editGroup(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("修改厂商");
		ReturnBody rb = ReturnBody.init();
		rb = keywordGMemberService.updateGroupById(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}
	
	/**
	 * 进入价格和类型修改页面
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("/editPricePage")
	public String editPricePage(@RequestParam Map<String, Object> pam, HttpServletRequest request) {
		log.info("进入价格和类型页面");
		request.setAttribute("targetRanking", pam.get("targetRanking"));
		request.setAttribute("price", pam.get("price"));
		request.setAttribute("ids", pam.get("ids"));
		request.setAttribute("action", "editPrice");
		return "keyWord/proxy/price_edit";
	}
	
	@RequestMapping("/editPrice")
	public String editPrice(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("修改价格和类型");
		ReturnBody rb = ReturnBody.init();
		rb = keywordGMemberService.updatePriceAndTargetRanking(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}
	
	
	/**
	 * 启用停用
	 * @param pam
	 * @param session
	 * @return
	 */
	@RequestMapping("/keyWordEnable")
	@ResponseBody
	public String keyWordEnable(@RequestParam Map<String, Object> pam, HttpSession session) {
		log.info("进入关键词启用停用方法...");
		String msg = "";
		String oldState = (String) pam.get("oldState"); // 原来的状态
		String newState = (String) pam.get("newState"); // 现有的状态 也就是所要修改的状态
		if("01".equals(newState)){ // 如果现有状态 为01 使用中  那么原来的状态 必须为02 已停用
			if("02".equals(oldState)){
				pam.put("state", newState);
				try {
					keywordGMemberService.updatekeyWordReview(pam);
					msg = "true";
				} catch (Exception e) {
					msg = "error";
				}
			}
		}else if("02".equals(newState)){ // 如果现有状态 为02 已停用  那么原来的状态 必须为01  使用中
			if("01".equals(oldState)){
				pam.put("state", newState);
				try {
					keywordGMemberService.updatekeyWordReview(pam);
					msg = "true";
				} catch (Exception e) {
					msg = "error";
				}
			}
		}
		return msg;
	}

}
